<?php
// Load the QCubed Development Framework
require('../qcubed.inc.php');



class PersonListForm extends QForm {
    
    protected $dtgPeople;
    //Perm
    protected $authSys;
    protected function Form_Run() {
        $this->authSys = new Perm();
        $this->authSys->IsAuthenticated(true);
    }


//		protected function Form_Load() {}

    protected function Form_Create() {
        // Instantiate the Meta DataGrid
        $this->dtgPeople = new PersonDataGrid($this);

        // Style the DataGrid (if desired)
        //$this->dtgPeople->CssClass = 'datagrid';
        $this->dtgPeople->AlternateRowStyle->CssClass = 'alternate';
        $this->dtgPeople->SetDataBinder("dtg_Bind");
        // Add Pagination (if desired)
        $this->dtgPeople->Paginator = new QPaginator($this->dtgPeople);
        $this->dtgPeople->ItemsPerPage = 20;

        $this->dtgPeople->AddRowAction(new QMouseOverEvent(), new QCssClassAction('selectedStyle'));
        $this->dtgPeople->AddRowAction(new QMouseOutEvent(), new QCssClassAction());

        $this->dtgPeople->RowActionParameterHtml = '<?= $_ITEM->Id ?>';
        $this->dtgPeople->AddRowAction(new QClickEvent(), new QAjaxAction('dtgPeopleRow_Click'));


        $this->dtgPeople->MetaAddColumn('FirstName');
        $this->dtgPeople->MetaAddColumn('LastName');
        $this->dtgPeople->MetaAddColumn(QQN::Person()->Org->Name);


    }
    protected function dtg_Bind() {
        $optClose = array( $this->dtgPeople->OrderByClause, $this->dtgPeople->LimitClause);
        /*@PERM*/
        if ( $_SESSION[_SV_PERM_][_SV_PERM_Admin_Role_]== true ) {
            $this->dtgPeople->TotalItemCount = Person::QueryCount($this->dtgPeople->Conditions);
            $this->dtgPeople->DataSource = Person::QueryArray($this->dtgPeople->Conditions,
                    QQ::Clause(
                    $this->dtgPeople->OrderByClause,
                    $this->dtgPeople->LimitClause
            ));
        }
        else {
            $this->dtgPeople->TotalItemCount = Person::CountByOrgArray($this->dtgPeople->Conditions);
            $this->dtgPeople->DataSource = Person::LoadArrayByOrgArray($this->dtgPeople->Conditions,
                    QQ::Clause(
                    $this->dtgPeople->OrderByClause,
                    $this->dtgPeople->LimitClause
            ));
        }
    }

    protected function dtgPeopleRow_Click($strFormId, $strControlId, $strParameter) {
        $intId = intval($strParameter);
        $strEditPageUrl = __PERSON__. '/person_edit.php/'. $intId;
        QApplication::Redirect($strEditPageUrl);
    }

}

// Go ahead and run this form object to generate the page and event handlers, implicitly using
// person_list.tpl.php as the included HTML template file
PersonListForm::Run('PersonListForm','tpl/person_list.tpl.php');


?>